<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public function index(){
		$data['main_view']='admin/main';
		$this->load->view('admin/login',$data);
	}
	
	
	function __construct() {
    parent::__construct();	
		$this->load->model('login_model');
	}

	function doLogin(){
		$username   = $this->input->post('username');
    $password   = $this->input->post('password');
		
		$this->form_validation->set_rules('username','Username','required');
		$this->form_validation->set_rules('password','Password','required');
		$this->form_validation->set_message('required','%s harus diisi');
		if($this->form_validation->run()==false){
			$this->index();
		}else{
    	$pass   = md5($password);
    	$cek=$this->login_model->doLogin($username,$pass);
    	if($cek!=false){
				if($this->login_model->cekStatus($cek['id_user'])){
     			$data = array (
        		'isLogin' => 'yes',
						'id'=>$cek['id_user'],
        		'user_id' => $cek['username'],
						'nama' => $cek['nama'],
     			);
      		$this->session->set_userdata($data);
      		redirect('dashboard');
				}else{
					$this->session->set_flashdata('error_login', 'Username ini sudah tidak Aktif !');
          redirect('login');
				}
      }else{
				$this->session->set_flashdata('error_login', 'Username atau password anda salah !');
        redirect('login');
      }
		}
	}
	
	function logOut(){
		$this->session->unset_userdata('isLogin');
		$this->session->unset_userdata('user_id');
		$this->session->sess_destroy();
		$this->session->set_flashdata('error_login', 'Anda Sudah Log Out!');
		redirect('login');
	}	
}